﻿using System;
using Transformable_Engine_v2.Engine.Global;

namespace Transformable_Engine_v2.Engine.Animation.Queue
{
    public class AnimationQueue : BaseAnimation
    {
        public BaseAnimation[] Animations { get; private set; }
        protected int CurrentAnimation;
        public AnimationQueue(double delay, bool autoStart, params BaseAnimation[] animations)
            : base(0, false, delay, autoStart)
        {
            Animations = animations;
            CurrentAnimation = -1;
            IsStarted = false;
        }

        protected override void DelayEnd()
        {
            ChangeCurrentAnimationIndex();
            IsStarted = true;
            base.DelayEnd();
        }

        protected virtual void ChangeCurrentAnimationIndex()
        {
            Log.LogDebug("Changing frame");

            CurrentAnimation++;
            
            if (CurrentAnimation == Animations.Length)
                Die();
            else
                Animations[CurrentAnimation].Start();
        }

        public override void Update(TimeSpan gameTime)
        {
            if(IsStarted && CurrentAnimation < Animations.Length)
                Animations[CurrentAnimation].Update(gameTime);
        }

        public override void FixedStepUpdate(TimeSpan gameTime)
        {
            if (IsStarted && CurrentAnimation < Animations.Length)
            {
                Animations[CurrentAnimation].FixedStepUpdate(gameTime);

                if (!Animations[CurrentAnimation].IsAlive())
                    ChangeCurrentAnimationIndex();
            }

            base.FixedStepUpdate(gameTime);
        }
    }
}